package com.terrynoya.fight.animations
{
	import com.terrynoya.coreLib.data.collection.MArrayList;

	public class Animation implements IAnimation
	{
		private var _id:int;
		
		private var _elements:Array;
		
		private var _loopStartId:int;
		
		private var _totalTime:int;
		
		public function Animation(id:int,loopStartId:int = 0,elements:Array = null)
		{
			this._id = id;
			this._loopStartId = loopStartId;
			this.setElements(elements);
		}
		
		public function get loopStartId():int
		{
			return _loopStartId;
		}

		public function set loopStartId(value:int):void
		{
			_loopStartId = value;
		}

		public function get elementLength():int
		{
			return this._elements.length;
		}
		
		public function setElements(value:Array):void
		{
			if(value == null)
			{
				return;
			}
			this._elements = value;
			this._totalTime = this.calcTotalTime();
		}
		
		public function getElement(index:int):IAnimationElement
		{
			return this._elements[index];
		}

		public function get id():int
		{
			return _id;
		}

		public function set id(value:int):void
		{
			_id = value;
		}
		
		public function getNextElement(elementId:int):IAnimationElement
		{
			var rlt:IAnimationElement;
			elementId++;
			if(elementId <= this._elements.length - 1)
			{
				rlt = this._elements[elementId];
			}
			else
			{
				rlt = this._elements[this._loopStartId];	
			}
			return rlt;
		}
		
		public function getElementStartTime(index:int):int
		{
			return AnimationElement(this._elements[index]).startTick;
		}
		
		public function get totalTime():int
		{
			return _totalTime;
		}
		
		protected function calcTotalTime():int
		{
			var time:int = 0;
			for (var i:int = 0; i < this._elements.length; i++) 
			{
				var ele:IAnimationElement = this._elements[i];
				if(ele.ticks == -1)
				{
					time = -1;
					break;
				}
				time += ele.ticks;
			}
			return time;
		}
	}
}